﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Win32;
using ShapeConnectors.MaTrix;
using System.Collections.Generic;
using System;
using System.Windows.Threading;
using System.Threading;
namespace ShapeConnectors
{
    public partial class Window1 : Window
    {
        // flag for enabling "New thumb" mode
        bool isAddNewAction = false;
        // flag for enabling "New link" mode
        bool isAddNewLink = false;
        // flag that indicates that the link drawing with a mouse started
        bool isLinkStarted = false;
        // variable to hold the thumb drawing started from
        MyThumb linkedThumb;//đỉnh
        // Line drawn by the mouse before connection established
        //LineGeometry link;//1 đường dẫn từ a->b
        Canh link;
        List<MyThumb> dsDinh = new List<MyThumb>();
        List<Canh> dsCanh = new List<Canh>();
        //cờ xóa đỉnh
        bool xoadinh = false;
        //cờ xóa cạnh
        bool xoacanh = false;
        
        


        public Window1()
        {
            InitializeComponent();
            
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Setup connections for predefined thumbs            

            //xử lý cho phép đặt 1 đỉnh mới
            //this.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Window1_PreviewMouseLeftButtonDown);
            //hiện đường kéo chuột
            //this.PreviewMouseMove += new MouseEventHandler(Window1_PreviewMouseMove);
            //xử lý cho nét vẽ đến có thể nhấp vào đỉnh kết thúc
            //this.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Window1_PreviewMouseLeftButtonUp);
            myCanvas.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(myCanvas_PreviewMouseLeftButtonDown);
            myCanvas.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(myCanvas_PreviewMouseLeftButtonUp);
            myCanvas.PreviewMouseMove += new MouseEventHandler(myCanvas_PreviewMouseMove);
            this.Title = "Mo Phong Thuat Toan ";
            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;
            bt_NapLai.IsEnabled = false;
        }

        // Event hanlder for dragging functionality support
        //hàm di di chuyen dinh trên form
        private void onDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            // Exit dragging operation during adding new link
            if (isAddNewLink) return;

            MyThumb thumb = e.Source as MyThumb;//xac dinh 1 đỉnh đang chọn

            Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);//cập nhật lại tọa độ đan xét
            Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);

            // Update links' layouts for active thumb
            thumb.UpdateLinks();//cập nhật lại danh sách các line nối với đỉnh đó
            int x = int.Parse(thumb.Title);
            for (int i = 0; i < dsCanh.Count; i++)
            {
                if (dsCanh[i].U == x || dsCanh[i].V == x)
                {
                    dsCanh[i].Updatelb();
                }
            }
        }
        private void myCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (isAddNewAction)
            {
                // Create new thumb object based on a static template "BasicShape1"
                // specifying thumb text as "action" and icon as "/Images/gear_connection.png"
                Point p = e.GetPosition(myCanvas);
                MaTran.CapNhatDinhMaTran();
                taodinh(MaTran.spt - 1, p);
                isAddNewAction = false;
                Mouse.OverrideCursor = null;
                btnNewAction.IsEnabled = btnNewLink.IsEnabled = true;
                e.Handled = true;
            }

            // Is adding new link and a thumb object is clicked...
            //tạo đường dẫn 2 đỉnh bằng chuột
            if (isAddNewLink && e.Source.GetType() == typeof(MyThumb))//e.Source.GetType() là xác định dc đỉnh đang click
            {
                if (!isLinkStarted)
                {

                    if (link == null || link.Line.EndPoint != link.Line.StartPoint)
                    {
                        link = new Canh();
                        Point position = e.GetPosition(myCanvas);//lấy tọa độ chuột


                        link.Line = new LineGeometry(position, position);//khi bắt đầu nhấp vào đỉnh để tạo cạnh thì tọa độ đầu và cuối là như nhau

                        connectors.Children.Add(link.Line);//chứa link 


                        isLinkStarted = true;
                        linkedThumb = e.Source as MyThumb;
                        link.U = int.Parse(linkedThumb.Title);//add đỉnh đầu vào dscanh
                        e.Handled = true;
                    }
                }

            }
            if (xoadinh == true)
            {
                MyThumb chondinh = e.Source as MyThumb;
                if (chondinh == null)
                    return;

                int x = int.Parse(chondinh.Title);
                MaTran.XoaDinh(x);
                for (int i = dsCanh.Count - 1; i >= 0; i--)
                {
                    if (dsCanh[i].U == x || dsCanh[i].V == x)
                    {

                        connectors.Children.Remove(dsCanh[i].Line);
                        myCanvas.Children.Remove(dsCanh[i].TextTS);
                        MaTran.XoaCanh(dsCanh[i].U, dsCanh[i].V);
                        dsCanh.RemoveAt(i);

                    }
                }
                MaTran.CapNhatCanhKhiXoaDinh(x);
                CapNhatChiSoDinh(x);
                myCanvas.Children.Remove(chondinh);
                dsDinh.Remove(chondinh);
                MaTran.ChuyenDSCanhThanhMaTran();
                xoadinh = false;
                
                bt_xoadinh.IsEnabled = true;
            }
            if (xoacanh == true)
            {
                Point p = e.GetPosition(myCanvas);
                XoaCanh(p);

                xoacanh = false;
                bt_xoacanh.IsEnabled = true;
            }
        }
        private void myCanvas_PreviewMouseMove(object sender, MouseEventArgs e)
        {

            if (isAddNewLink && isLinkStarted)
            {
                // Set the new link end point to current mouse position
                link.Line.EndPoint = e.GetPosition(myCanvas);//đỉnh kết thúc thay đổi

                e.Handled = true;
            }
        }
        private void myCanvas_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // If "Add link" mode enabled and line drawing started (line placed to canvas)
            if (isAddNewLink && isLinkStarted)
            {
                // declare the linking state
                bool linked = false;
                // We released the button on MyThumb object
                if (e.Source.GetType() == typeof(MyThumb))
                {
                    MyThumb targetThumb = e.Source as MyThumb;
                    // define the final endpoint of line
                    link.Line.EndPoint = e.GetPosition(myCanvas);
                    // if any line was drawn (avoid just clicking on the thumb)
                    if (link.Line.EndPoint != link.Line.StartPoint && linkedThumb != targetThumb)
                    {
                        // establish connection
                        linkedThumb.LinkTo(targetThumb, link.Line);//nối 2 đỉnh

                        //nhập trọng số cho cạnh
                        link.V = int.Parse(targetThumb.Title);
                        NhapTrongSo nhapTS = new NhapTrongSo();
                        nhapTS.ShowDialog();
                        link.Trongso = nhapTS.TrongSo;

                        link.Updatelb();
                        myCanvas.Children.Add(link.TextTS);
                        MaTran.CapNhatCanhMaTran(link.U, link.V, link.Trongso);
                        dsCanh.Add(link);
                        // set linked state to true
                        linked = true;
                    }
                }
                // if we didn't manage to approve the linking state
                // button is not released on MyThumb object or double-clicking was performed
                if (!linked)
                {
                    // remove line from the canvas
                    connectors.Children.Remove(link.Line);
                    // clear the link variable
                    link = null;
                }

                // exit link drawing mode
                isLinkStarted = isAddNewLink = false;
                // configure GUI
                btnNewAction.IsEnabled = btnNewLink.IsEnabled = true;

                Mouse.OverrideCursor = null;
                e.Handled = true;
            }

           
            Mouse.OverrideCursor = null;
        }

        void taodinh(int i, Point p)
        {
            MyThumb newThumb = new MyThumb(
            Application.Current.Resources["BasicShape1"] as ControlTemplate,
            i.ToString(),
            "/Images/Orb_Icons_006.png",
            p,//tọa độ của chuột là tọa độ của đỉnh,
            onDragDelta);
            myCanvas.Children.Add(newThumb);
            dsDinh.Add(newThumb);
        }
        void taocanh()
        {
            for (int i = 0; i < MaTran.spt; i++)
                for (int j = i + 1; j < MaTran.spt; j++)
                {
                    if (MaTran.a[i, j] != 0)
                    {
                        link = new Canh();
                        link.Line = new LineGeometry(dsDinh[i].P, dsDinh[j].P);
                        dsDinh[i].LinkTo(dsDinh[j], link.Line);
                        link.U = i;
                        link.V = j;
                        link.Trongso = MaTran.a[i, j];

                        dsCanh.Add(link);

                        connectors.Children.Add(link.Line);
                        link.Updatelb();
                        myCanvas.Children.Add(link.TextTS);
                    }
                }
        }
        private void TaoDoThi()
        {
            float radius = 0.017453293f;
            Point p = new Point();
            float goc = (float)(360 / MaTran.spt);
            for (int i = 0; i < MaTran.spt; i++)
            {

                p.X = (float)130f * (float)Math.Sin((double)(goc * i * radius));
                p.Y = (float)130f * (float)Math.Cos((double)(goc * i * radius));
                p.X += 200f;
                p.Y += 180f;
                taodinh(i, p);
            }
            taocanh();

        }

        //========input===output============================================
        private void bt_MoMaTran_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text|*.txt|All File()|*.*";
            ofd.Title = "Open File...";
            if (ofd.ShowDialog() == true)
            {
                clear();
                MaTran.Docfile(ofd.FileName);
                TaoDoThi();
            }
            bt_Prim.IsEnabled = true;
            bt_Kruskal.IsEnabled = true;
            bt_TPLT.IsEnabled = true;
            bt_Dijkstra.IsEnabled = true;
            br_Euler.IsEnabled = true;
        }
        private void bt_MoDanhSachCanh_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Text|*.txt|All File()|*.*";
            ofd.Title = "Open File...";
            if (ofd.ShowDialog() == true)
            {
                clear();
                MaTran.Docfile2(ofd.FileName);
                TaoDoThi();
            }
            
            bt_Prim.IsEnabled = true;
            bt_Kruskal.IsEnabled = true;
            bt_TPLT.IsEnabled = true;
            bt_Dijkstra.IsEnabled = true;
            br_Euler.IsEnabled = true;
            
        }
        private void bt_PhatSinhMaTran_Click(object sender, RoutedEventArgs e)
        {

            input _input = new input();
            _input.ShowDialog();
            MaTran.PhatSinh(_input.dinh, _input.canh);
            clear();
            TaoDoThi();
            bt_Prim.IsEnabled = true;
            bt_Kruskal.IsEnabled = true;
            bt_TPLT.IsEnabled = true;
            bt_Dijkstra.IsEnabled = true;
            br_Euler.IsEnabled = true;
            

        }
        private void bt_LuuMaTran_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog ofd = new SaveFileDialog();
            ofd.Filter = "Text|*.txt|All File()|*.*";
            ofd.Title = "Save File...";
            if (ofd.ShowDialog() == true)
            {
                MaTran.Ghifile(ofd.FileName);

            }
        }

        //========Các chức năng=============================================
        private void btnNewAction_Click(object sender, RoutedEventArgs e)
        {
            isAddNewAction = true;
            Mouse.OverrideCursor = Cursors.SizeAll;//doi kieu chuot
            btnNewAction.IsEnabled = false;//button chìm xuống
        }
        private void btnNewLink_Click(object sender, RoutedEventArgs e)
        {

            isAddNewLink = true;
            //isAddNewAction = false;
            Mouse.OverrideCursor = Cursors.Pen;
            btnNewLink.IsEnabled = false;
        }
        private void bt_xoadinh_Click(object sender, RoutedEventArgs e)
        {

            xoadinh = true;
            Mouse.OverrideCursor = Cursors.Hand;
            bt_xoadinh.IsEnabled = false;
        }
        private void bt_xoatrang_Click(object sender, RoutedEventArgs e)
        {
            clear();
            MaTran.Reset();
        }
        private void bt_xoacanh_Click(object sender, RoutedEventArgs e)
        {
            xoacanh = true;
            Mouse.OverrideCursor = Cursors.Hand;
            bt_xoacanh.IsEnabled = false;
        }



        public void CapNhatChiSoDinh(int x)
        {
            for (int i = x + 1; i < dsDinh.Count; i++)
            {
                dsDinh[i].Title = (i - 1).ToString();
                dsDinh[i].UpdateTitle((i - 1).ToString());

            }
            for (int i = 0; i < dsCanh.Count; i++)
            {
                if (dsCanh[i].U > x)
                    dsCanh[i].U--;
                if (dsCanh[i].V > x)
                    dsCanh[i].V--;
            }
        }
        private void XoaCanh(Point p)
        {
            for (int i = 0; i < dsCanh.Count; i++)
            {
                double dauX = dsCanh[i].Line.StartPoint.X;
                double dauY = dsCanh[i].Line.StartPoint.Y;
                double cuoiX = dsCanh[i].Line.EndPoint.X;
                double cuoiY = dsCanh[i].Line.EndPoint.Y;
                double kc = Math.Sqrt((cuoiX - dauX) * (cuoiX - dauX) + (cuoiY - dauY) * (cuoiY - dauY));
                double kc1 = Math.Sqrt((p.X - dauX) * (p.X - dauX) + (p.Y - dauY) * (p.Y - dauY));
                double kc2 = Math.Sqrt((cuoiX - p.X) * (cuoiX - p.X) + (cuoiY - p.Y) * (cuoiY - p.Y));
                if (Math.Abs(kc1 + kc2 - kc) <= 1)
                {
                    connectors.Children.Remove(dsCanh[i].Line);
                    MaTran.XoaCanh(dsCanh[i].U, dsCanh[i].V);
                    myCanvas.Children.Remove(dsCanh[i].TextTS);
                    dsCanh.Remove(dsCanh[i]);
                    MaTran.ChuyenDSCanhThanhMaTran();

                }
            }
        }
        private void clear()
        {
            while (dsDinh.Count != 0)
            {
                myCanvas.Children.Remove(dsDinh[0]);
                dsDinh.RemoveAt(0);
            }
            while (dsCanh.Count != 0)
            {
                connectors.Children.Remove(dsCanh[0].Line);
                myCanvas.Children.Remove(dsCanh[0].TextTS);
                dsCanh.RemoveAt(0);
            }

            connectors2.Children.Clear();


        }
        int u = 0;
        BuocThucHien buoc;
        DispatcherTimer Dtimer;
        DispatcherTimer timer;
        public void ToMauPrim()
        {
            myCanvas.Children.Remove(dsDinh[0]);
            dsDinh[0].UpdateImage("/Images/Orb_Icons_004.png");
            myCanvas.Children.Add(dsDinh[0]);
            Dtimer = new DispatcherTimer();
            Dtimer.Tick += new EventHandler(Dtimer_Tick);
            Dtimer.Interval = new TimeSpan(0, 0, 1);
            Dtimer.Start();
        }
        
        void Dtimer_Tick(object sender, EventArgs e)
        {
            buoc = new BuocThucHien();
            buoc = Prim.StepPrim[u];
            for (int i = 0; i < buoc.Canhlienquan.Count; i++)
            {
                int d = buoc.Canhlienquan[i].GetDinh1();
                int c = buoc.Canhlienquan[i].GetDinh2();
                for (int j = 0; j < dsCanh.Count; j++)
                {
                    if ((dsCanh[j].U == d && dsCanh[j].V == c) || (dsCanh[j].U == c && dsCanh[j].V == d))
                    {
                        connectors2.Children.Add(dsCanh[j].Line);

                    }

                }
            }
            Dtimer.Stop();
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();
            u++;
            if (u >= Prim.StepPrim.Count)
                Dtimer.Stop();
        }
        List<Canh> dsCanhCanTo ;

        void timer_Tick(object sender, EventArgs e)
        {
            
            connectors2.Children.Clear();
            int x = buoc.Canhcanto.GetDinh1();
            int y = buoc.Canhcanto.GetDinh2();
            for (int i = 0; i < buoc.Canhlienquan.Count; i++)
            {
                for (int j = 0; j < dsCanh.Count; j++)
                {
                    if ((dsCanh[j].U == x && dsCanh[j].V == y) || (dsCanh[j].U == y && dsCanh[j].V == x))
                    {
                        dsCanhCanTo.Add(dsCanh[j]);
                    }
                }
            }
            for (int i = 0; i < dsCanhCanTo.Count; i++)
            {
                connectors2.Children.Add(dsCanhCanTo[i].Line);
                myCanvas.Children.Remove(dsDinh[buoc.Dinhcanto]);
                dsDinh[buoc.Dinhcanto].UpdateImage("/Images/Orb_Icons_004.png");
                myCanvas.Children.Add(dsDinh[buoc.Dinhcanto]);
            }
            int trongso = 0;
            string chuoi = null;
            chuoi = "Cây tối đại ngắn nhất:\n";
            for (int i = 0; i < buoc.KetQuaPrim.Count; i++)
            {
                int a = buoc.KetQuaPrim[i].GetDinh1();
                int b = buoc.KetQuaPrim[i].GetDinh2();
                int c = buoc.KetQuaPrim[i].GetTS();
                chuoi  =chuoi + a.ToString() + "->" + b.ToString() + " :" + c.ToString() + "\n";
                trongso += c;
            }
            chuoi = chuoi + "\nTổng trọng số :" + trongso.ToString();
            tb_KetQua.Text = chuoi;
            Thread.Sleep(200);
            if (u >= Prim.StepPrim.Count)
            {
                Dtimer.Stop();
                bt_NapLai.IsEnabled = true;
            }
            else
                Dtimer.Start();
            timer.Stop();
        }

        private void bt_Prim_Click(object sender, RoutedEventArgs e)
        {
            bt_NapLai.IsEnabled = false;
            tb_KetQua.Clear();
            Prim pr = new Prim();
            string chuoi = pr.XuatPrim();
            if (chuoi == "Đồ thị không liên thông")
            {
                tb_KetQua.Text = chuoi;
                MessageBox.Show("Đồ thị không liên thông");
                return;
            }
            dsCanhCanTo = new List<Canh>();
            u = 0;
            ToMauPrim();
            dsCanhCanTo.Clear();
            btnNewAction.IsEnabled = false;
            btnNewLink.IsEnabled = false;
            bt_xoadinh.IsEnabled = false;
            bt_xoacanh.IsEnabled = false;
            bt_xoatrang.IsEnabled = false;
            bt_NapLai.IsEnabled = false;

            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;

            bt_MoMaTran.IsEnabled = false;
            bt_MoDanhSachCanh.IsEnabled = false;
            bt_PhatSinhMaTran.IsEnabled = false;
            bt_LuuMaTran.IsEnabled = false;
        }

        //To mau TPLT
        #region
        DispatcherTimer Dtimer_TPLT;
        DispatcherTimer timer2;
        List<Canh> dsCanhCanTo2 = new List<Canh>();
        
        public void ToMauTPLT()
        {
            Hinh.addhinh();
            Dtimer_TPLT = new DispatcherTimer();
            Dtimer_TPLT.Tick += new EventHandler(Dtimer_TPLT_Tick);
            Dtimer_TPLT.Interval = new TimeSpan(0, 0, 1);
            Dtimer_TPLT.Start();
        }
       
        void Dtimer_TPLT_Tick(object sender, EventArgs e)
        {
            buoc = new BuocThucHien();
            buoc = TPLT.Steptplt[u];
            myCanvas.Children.Remove(dsDinh[buoc .DinhChon]);
            dsDinh[buoc.DinhChon].UpdateImage(Hinh.dsHinh[buoc.Label-1]);
            myCanvas.Children.Add(dsDinh [buoc .DinhChon]);
            Thread.Sleep(200);
            timer2 = new DispatcherTimer();
            timer2.Tick += new EventHandler(timer2_Tick);
            timer2.Interval = new TimeSpan(0, 0, 1);
            timer2.Start();
            u++;
            if (u >=TPLT .Steptplt.Count)
                Dtimer_TPLT .Stop();
            else
                Dtimer_TPLT .Start();
            
           
        }

        void timer2_Tick(object sender, EventArgs e)
        {
            //connectors2.Children.Clear();
            //int x = buoc.Canhcanto.GetDinh1();
            //int y = buoc.Canhcanto.GetDinh2();
           
            //for (int j = 0; j < dsCanh.Count; j++)
            //{
            //    if ((dsCanh[j].U == x && dsCanh[j].V == y) || (dsCanh[j].U == y && dsCanh[j].V == x))
            //    {
            //        dsCanhCanTo.Add(dsCanh[j]);
            //    }
            //}
           
            //for (int i = 0; i < dsCanhCanTo.Count; i++)
            //{
            //    connectors2.Children.Add(dsCanhCanTo[i].Line);
            //    myCanvas.Children.Remove(dsDinh[buoc.Dinhcanto]);
            //    dsDinh[buoc.Dinhcanto].UpdateImage("/Images/Orb_Icons_004.png");
            //    myCanvas.Children.Add(dsDinh[buoc.Dinhcanto]);
            //}
            if (buoc.Dinhcanto != -1)
            {
                myCanvas.Children.Remove(dsDinh[buoc.Dinhcanto]);
                dsDinh[buoc.Dinhcanto].UpdateImage(Hinh.dsHinh[buoc.Label - 1]);
                myCanvas.Children.Add(dsDinh[buoc.Dinhcanto]);
            }
            Thread.Sleep(200);

            if (u >=TPLT .Steptplt.Count)
                Dtimer_TPLT.Stop();
            else
                Dtimer_TPLT.Start();
            timer2.Stop();
        }

     
        private void bt_TPLT_Click(object sender, RoutedEventArgs e)
        {
            tb_KetQua.Clear();
            TPLT lt = new TPLT();
            tb_KetQua.Text = lt.FindConnected();
            ToMauTPLT();
            
        }

        #endregion

        private void bt_Kruskal_Click(object sender, RoutedEventArgs e)
        {
            tb_KetQua.Clear();
            Kruskal kr = new Kruskal();
            int kq = kr.KruskalAlg();
            int lt = kr.KTLT();
            if (kq == 1 && lt == 1)
            {
                u = 0;
                ToMauKruskal();
                
            }
            else
            {
                tb_KetQua.Text = "Đồ thị không liên thông";
                MessageBox.Show("Đồ thị không liên thông");
                return;
            }

            btnNewAction.IsEnabled = false;
            btnNewLink.IsEnabled = false;
            bt_xoadinh.IsEnabled = false;
            bt_xoacanh.IsEnabled = false;
            bt_xoatrang.IsEnabled = false;
            bt_NapLai.IsEnabled = false;

            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;

            bt_MoMaTran.IsEnabled = false;
            bt_MoDanhSachCanh.IsEnabled = false;
            bt_PhatSinhMaTran.IsEnabled = false;
            bt_LuuMaTran.IsEnabled = false;

        }
<<<<<<< .mine
        //DispatcherTimer DijkTimer;
=======
        DispatcherTimer krtimer;
        private void ToMauKruskal()
        {
            krtimer = new DispatcherTimer();
            krtimer.Tick += new EventHandler(krtimer_Tick);
            krtimer.Interval = new TimeSpan(0,0,1);
            krtimer.Start();
        }
        
        void krtimer_Tick(object sender, EventArgs e)
        {
            
            buoc = new BuocThucHien();
            buoc = Kruskal.BuocKruskal[u];
            myCanvas.Children.Remove(dsDinh[buoc.Dinhdau]);//xoa dinh cu
            dsDinh[buoc.Dinhdau].UpdateImage("/Images/Orb_Icons_020.png");
            myCanvas.Children.Add(dsDinh[buoc.Dinhdau]);
            
            int d=buoc.Canhcanto.GetDinh1();
            int c=buoc.Canhcanto.GetDinh2();
            for (int i = 0; i < dsCanh.Count; i++)
            {
                if ((d == dsCanh[i].U && c == dsCanh[i].V) || (d == dsCanh[i].V && c == dsCanh[i].U))
                    connectors2.Children.Add(dsCanh[i].Line);
            }
            
            int trongso = 0;
            string chuoi = null;
            chuoi = "Cây tối đại ngắn nhất:\n";
            for (int i = 0; i < buoc.Kqkruskal.Count; i++)
            {
                int q = buoc.Kqkruskal[i].GetDinh1();
                int w = buoc.Kqkruskal[i].GetDinh2();
                int s = buoc.Kqkruskal[i].GetTS();
                chuoi = chuoi + q.ToString() + "->" + w.ToString() + " :" + s.ToString()+"\n";
                trongso += s;
            }
            
            chuoi = chuoi + "\nTổng trọng số :" + trongso.ToString();
            tb_KetQua.Text = chuoi;
            myCanvas.Children.Remove(dsDinh[buoc.Dinhcuoi]);//xoa dinh cu
            dsDinh[buoc.Dinhcuoi].UpdateImage("/Images/Orb_Icons_020.png");
            myCanvas.Children.Add(dsDinh[buoc.Dinhcuoi]);

>>>>>>> .r35
            Thread.Sleep(200);
            u++;
            if (u >= Kruskal.BuocKruskal.Count)
            {
                krtimer.Stop();
                bt_NapLai.IsEnabled = true;
            }
        }
        private void bt_Dijkstra_Click(object sender, RoutedEventArgs e)
        {
           // DijkTimer = new DispatcherTimer();
           
            NhapDijkstra fr = new NhapDijkstra();
            fr.ShowDialog();
            Dijkstra d = new Dijkstra();
            d.Nhap(fr.start, fr.end);
            d.DijkstraAlg(fr.start);
            tb_KetQua.Text = d.XuatDijkstra();
            fr.Close();
<<<<<<< .mine
            //DijkTimer.Tick += new EventHandler(DijkTimer_Tick);
            //DijkTimer.Interval = new TimeSpan(0, 0, 1);
            //DijkTimer.Start();
            
=======
            btnNewAction.IsEnabled = false;
            btnNewLink.IsEnabled = false;
            bt_xoadinh.IsEnabled = false;
            bt_xoacanh.IsEnabled = false;
            bt_xoatrang.IsEnabled = false;
            bt_NapLai.IsEnabled = false;

            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;

            bt_MoMaTran.IsEnabled = false;
            bt_MoDanhSachCanh.IsEnabled = false;
            bt_PhatSinhMaTran.IsEnabled = false;
            bt_LuuMaTran.IsEnabled = false;
>>>>>>> .r36
        }

        //void DijkTimer_Tick(object sender, EventArgs e)
        //{
            
        //    int d = Dijkstra .stepDisjtra [u].Canhcanto.GetDinh1();
        //    int c= Dijkstra.stepDisjtra[u].Canhcanto.GetDinh2();
        //    for (int i = 0; i < Dijkstra.stepDisjtra[u].Canhlienquan.Count; i++)
        //    {

        //        int d1, c1;
        //        d1 = Dijkstra .stepDisjtra [u].Canhlienquan[i].GetDinh1();
        //        c1 = Dijkstra .stepDisjtra [u].Canhlienquan[i].GetDinh2();
        //        for (int j = 0; j < dsCanh.Count; j++)
        //        {
        //            if ((dsCanh[j].U == d1 && dsCanh[j].V == c1) ||
        //                (dsCanh[j].U == c1 && dsCanh[j].V == d1))
        //            {
        //                connectors2.Children.Add(dsCanh[j].Line);
        //            }
        //        }
        //    }
        //        //DijkTimer.Stop();
        //        u++;
        //        if (u >=Dijkstra.stepDisjtra .Count)
        //            DijkTimer.Stop();
 
               
            
        //}

        private void br_Euler_Click(object sender, RoutedEventArgs e)
        {
            tb_KetQua.Clear();
            Euler el = new Euler();
            tb_KetQua.Text = el.XuatEuler();
            u = 0;
            ToMauEuler();

            btnNewAction.IsEnabled = false;
            btnNewLink.IsEnabled = false;
            bt_xoadinh.IsEnabled = false;
            bt_xoacanh.IsEnabled = false;
            bt_xoatrang.IsEnabled = false;
            bt_NapLai.IsEnabled = true;

            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;

            bt_MoMaTran.IsEnabled = false;
            bt_MoDanhSachCanh.IsEnabled = false;
            bt_PhatSinhMaTran.IsEnabled = false;
            bt_LuuMaTran.IsEnabled = false;
        }
        DispatcherTimer EulerTimer;
        DispatcherTimer eulerTimer2;
        private void ToMauEuler()
        {
            EulerTimer = new DispatcherTimer();
            EulerTimer.Tick += new EventHandler(EulerTimer_Tick);
            EulerTimer.Interval = new TimeSpan(0,0,1);
            EulerTimer.Start();

        }

        


        private void btQuayLui_Click(object sender, RoutedEventArgs e)
        {
        }


        void EulerTimer_Tick(object sender, EventArgs e)
        {
            buoc = new BuocThucHien();
            buoc = Euler.StepEuler[u];
            int d=buoc.Canhcanto.GetDinh1();
            int c=buoc.Canhcanto.GetDinh2();
            for (int i = 0; i < dsCanh.Count; i++)
            {
                if ((dsCanh[i].U == d && dsCanh[i].V == c) || (dsCanh[i].U == c && dsCanh[i].V == d))
                {
                    
                    connectors2.Children.Add(dsCanh[i].Line);
                }
            }       
            eulerTimer2 = new DispatcherTimer();
            eulerTimer2.Tick += new EventHandler(eulerTimer2_Tick);
            eulerTimer2.Interval = new TimeSpan(0, 0, 1);
            eulerTimer2.Start();
            u++;
            if (u >= Euler.StepEuler.Count)
                EulerTimer.Stop();
        }

        void eulerTimer2_Tick(object sender, EventArgs e)
        {
            //if (buoc.DinhDuongDi != -1)
            //{
            //    myCanvas.Children.Remove(dsDinh[buoc.DinhDuongDi]);
            //    dsDinh[buoc.DinhDuongDi].UpdateImage("/Images/Orb_Icons_004.png");
            //    myCanvas.Children.Add(dsDinh[buoc.DinhDuongDi]);
                

            //}
            Thread.Sleep(200);
            if (u >= Euler.StepEuler.Count)
                EulerTimer.Stop();
            else
                EulerTimer.Start();
            eulerTimer2.Stop();
        }
        //private void bt_TPLT_Click(object sender, RoutedEventArgs e)
        //{


        //}

        private void btDung_Click(object sender, RoutedEventArgs e)
        {
            btnNewAction.IsEnabled = false;
            btnNewLink.IsEnabled = false;
            bt_xoadinh.IsEnabled = false;
            bt_xoacanh.IsEnabled = false;
            bt_xoatrang.IsEnabled = false;
            bt_NapLai.IsEnabled = false;

            bt_Prim.IsEnabled = false;
            bt_Kruskal.IsEnabled = false;
            bt_TPLT.IsEnabled = false;
            bt_Dijkstra.IsEnabled = false;
            br_Euler.IsEnabled = false;

            bt_MoMaTran.IsEnabled = false;
            bt_MoDanhSachCanh.IsEnabled = false;
            bt_PhatSinhMaTran.IsEnabled = false;
            bt_LuuMaTran.IsEnabled = false;


        }

        private void btChayTiep_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btChayHet_Click(object sender, RoutedEventArgs e)
        {

        }


        private void bt_Back_Click(object sender, RoutedEventArgs e)
        {
            u--;
            connectors2.Children.Clear();
            for (int i = 0; i < u; i++)
            {
                ToMauPrim();

            }
        }
        bool PauseStart = false;
        private void bt_Pause_Click(object sender, RoutedEventArgs e)
        {
            
            if (PauseStart == false)
            {
                Dtimer.Stop();
                timer.Stop();
                bt_Pause.Content = "Start";
                PauseStart = true;
            }
            else
            {
                Dtimer.Start();
                timer.Start();
                bt_Pause.Content = "Pause";
                PauseStart = false;
            }

        }
<<<<<<< .mine

        private void bt_NapLai_Click(object sender, RoutedEventArgs e)
        {

        }

=======

        private void bt_NapLai_Click(object sender, RoutedEventArgs e)
        {
            clear();
            u = 0;  
            TaoDoThi();

            btnNewAction.IsEnabled = true;
            btnNewLink.IsEnabled = true;
            bt_xoadinh.IsEnabled = true;
            bt_xoacanh.IsEnabled = true;
            bt_xoatrang.IsEnabled = true;
            bt_NapLai.IsEnabled = true;
            bt_Kruskal.IsEnabled = true;
            bt_TPLT.IsEnabled = true;
            bt_Dijkstra.IsEnabled = true;
            br_Euler.IsEnabled = true;
            bt_Prim.IsEnabled = true;
            bt_MoMaTran.IsEnabled = true;
            bt_MoDanhSachCanh.IsEnabled = true;
            bt_PhatSinhMaTran.IsEnabled = true;
            bt_LuuMaTran.IsEnabled = true;
        }
>>>>>>> .r35
    }
}
